﻿@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <title>Create</title>
    <script src='@Url.Content("~/Scripts/PageCreater/Scripts/jquery-1.5.1.min.js")' type="text/javascript"></script>
    <script src='@Url.Content("~/Scripts/PageCreater/Scripts/PageCreater.js")' type="text/javascript"></script>
    <link href='@Url.Content("~/Scripts/PageCreater/Css/Style.css")' rel="stylesheet" type="text/css" />
    <script src='@Url.Content("~/Scripts/zTree/js/jquery-1.4.4.min.js")' type="text/javascript"></script>
    <link href='@Url.Content("~/Scripts/zTree/css/zTreeStyle/zTreeStyle.css")' rel="stylesheet" type="text/css" />
    <script src='@Url.Content("~/Scripts/zTree/js/jquery.ztree.core-3.5.js")' type="text/javascript"></script>
    <script src='@Url.Content("~/Scripts/zTree/js/jquery.ztree.excheck-3.5.js")' type="text/javascript"></script>
    <script src='@Url.Content("~/Scripts/zTree/js/jquery.ztree.exedit-3.5.js")' type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $.ajaxSetup({ cache: false });
            var str = "Account,账号,false,text,notnull,"
                        + "Password,密码,false,password,notnull,"
                        + "IsActive,状态,false,select|激活!true|注销!false,notnull,"
                        + "Opreate,操作,false,null,null";
            CreatePageFram(str);
            $.fn.zTree.init($("#roleTree"), roleSetting, roleNodes);
        })

        var roleSetting = {
            async: {
                enable: true,
                url: "/User/GetAllRoles",
                type: "Get",
                dataType: "json",
                autoParam: ["id","pid","name","level"],  
            },
            check: {
                enable: true
            },
            callback: {
                onAsyncSuccess: function (event, treeId, treeNode, msg) {
                },
                onAsyncError: function (event, treeId, treeNode, XMLHttpRequest, textStatus, errorThrown) {
                    alert("加载错误，请联系系统管理员！");
                },
                beforeExpand: beforeExpand,
				onExpand: onExpand,
				onClick: onClick,
                onCheck: function(event, treeId, treeNode){
                    if(treeNode.checked==true){
                        if(treeNode.level==0){
                            if(treeNode.children==null)
                            {
                                treeNode.checked=false;
                                alert("请先展开列表！");
                            }
                        }
                    }
                }
            }
        };
        function createNodes(maxNodesNumInLevel, maxLevel, curLevel, curPId) {
			if (maxNodesNumInLevel<5) {
				maxNodesNumInLevel = 5;
			}
			var nodes = [], num = 0;
			while(num<3) {
				num = parseInt(Math.random()*1024)%maxNodesNumInLevel+1;
			}
			for (var i=0; i<num; i++) {
				var id = curPId ? curPId + "-" + i : "" + i, isParent = (parseInt(Math.random()*9999)%3!=0),
				node = {id: id, pId : curPId, name : "N" + id};
				nodes.push(node);
				if (isParent && curLevel<maxLevel) {
					nodes = nodes.concat(createNodes(maxNodesNumInLevel, maxLevel, curLevel+1, id));
				}
			}
			return nodes;
		}
		var zNodes =createNodes(5, 5, 0);

		var curExpandNode = null;
		function beforeExpand(treeId, treeNode) {
			var pNode = curExpandNode ? curExpandNode.getParentNode():null;
			var treeNodeP = treeNode.parentTId ? treeNode.getParentNode():null;
			var zTree = $.fn.zTree.getZTreeObj("roleTree");
			for(var i=0, l=!treeNodeP ? 0:treeNodeP.children.length; i<l; i++ ) {
				if (treeNode !== treeNodeP.children[i]) {
					zTree.expandNode(treeNodeP.children[i], false);
				}
			}
			while (pNode) {
				if (pNode === treeNode) {
					break;
				}
				pNode = pNode.getParentNode();
			}
			if (!pNode) {
				singlePath(treeNode);
			}

		}
		function singlePath(newNode) {
			if (newNode === curExpandNode) return;
			if (curExpandNode && curExpandNode.open==true) {
				var zTree = $.fn.zTree.getZTreeObj("roleTree");
				if (newNode.parentTId === curExpandNode.parentTId) {
					zTree.expandNode(curExpandNode, false);
				} else {
					var newParents = [];
					while (newNode) {
						newNode = newNode.getParentNode();
						if (newNode === curExpandNode) {
							newParents = null;
							break;
						} else if (newNode) {
							newParents.push(newNode);
						}
					}
					if (newParents!=null) {
						var oldNode = curExpandNode;
						var oldParents = [];
						while (oldNode) {
							oldNode = oldNode.getParentNode();
							if (oldNode) {
								oldParents.push(oldNode);
							}
						}
						if (newParents.length>0) {
							zTree.expandNode(oldParents[Math.abs(oldParents.length-newParents.length)-1], false);
						} else {
							zTree.expandNode(oldParents[oldParents.length-1], false);
						}
					}
				}
			}
			curExpandNode = newNode;
		}

		function onExpand(event, treeId, treeNode) {
			curExpandNode = treeNode;
		}

		function onClick(e,treeId, treeNode) {
			var zTree = $.fn.zTree.getZTreeObj("roleTree");
			zTree.expandNode(treeNode, null, null, null, true);
		}
        var roleNodes = [
			{ id: 1, pid: 0, name: "请选择角色", isParent: true, icon: "../../Scripts/zTree/css/zTreeStyle/img/diy/users.png" }
		];

        function SubmitPage() {
            if (!InitValidator()) {
                return false;
            }
            var nodes = $.fn.zTree.getZTreeObj("roleTree").getCheckedNodes(true);
            var selectIDs = "";
            for (var i = 0; i < nodes.length; i++) {
                var xxx = nodes[i];
                selectIDs += nodes[i].tld + ",";
            }
            $.ajax({
                type: "Post",
                url: "/User/Create",
                data: { account: $("#Account").val(), password: $("#Password").val(), isActive: $("#IsActive").val(), selectedIds: selectIDs },
                success: function () {
                    alert("创建成功！");
                    window.location = "/User/Index";
                }
            });
        }
    </script>
</head>
<body>
    <div class="Content">
        <p>
            @Html.ActionLink("返回列表", "Index")
        </p>
        <p>
            <input id="btnSubmit" type="button" value="保存" class="btn3" onclick="SubmitPage()" /></p>
        <form id="Info" method="post">
        <div style="float: left; width: 30%;">
            <legend>请输入基本信息</legend>
            <div id="divContent">
            </div>
        </div>
        <div style="float: left; width: 30%;">
            <legend>请配置角色信息</legend>
            <ul id="roleTree" class="ztree">
            </ul>
        </div>
        </form>
    </div>
</body>
</html>
